package itsm.isperp.module.service.serviceCatalog;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.commons.repository.BaseRepository;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.solr.SolrSrcServiceDTO;
import itsm.isperp.module.entity.serviceCatalog.SrcService;
import itsm.isperp.module.repository.serviceCatalog.SrcServiceMapper;
import itsm.isperp.module.repository.solr.SolrSrcServiceRepository;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectServiceService;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.HighlightQuery;
import org.springframework.data.solr.core.query.SimpleFilterQuery;
import org.springframework.data.solr.core.query.SimpleHighlightQuery;
import org.springframework.data.solr.core.query.SimpleStringCriteria;
import org.springframework.stereotype.Service;

/**
 * 服务目录中服务的业务服务
 * 
 * @author zhg
 * @since V1.0
 * 
 */
@Service
public class SrcServiceService extends BaseService<SrcService> {

	@Autowired
	protected SrcServiceMapper srcServiceMapper;

	@Autowired
	protected SolrSrcServiceRepository solrSrcServiceRepository;

	@Override
	public BaseRepository<SrcService> getRepository() {
		return this.srcServiceMapper;
	}

	public DataResponse<SrcService> findCustomerSerciceByCusUserId(
			String cusUserId, JqGridRequest req) {
		if ("null".equals(cusUserId) || StringUtils.isEmpty(cusUserId)) {
			return null;
		}

		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");
		List<Map<String, Object>> projects = appUserService
				.findUserProjects(cusUserId);
		String projectIds = "";
		if (projects.size() > 0) {
			projectIds = "'" + projects.get(0).get("id").toString() + "'";

			for (int i = 0; i < projects.size(); i++) {
				Map<String, Object> m = projects.get(i);
				String id = (String) m.get("id");
				projectIds += ",'" + id + "'";
			}

		}
		ProjProjectServiceService projProjectServiceService = ContextHolder
				.getSpringBean("projProjectServiceService");

		return projProjectServiceService.getByProjectIds(projectIds, req);
	}

	/**
	 * 搜索服务目录（从服务目录的索引中搜索）
	 * 
	 * @param keyword
	 *            关键字
	 * 
	 * @return 服务目录分页列表
	 */
	public Page<SolrSrcServiceDTO> searchServiceByKeyword(String keyword,
			String projectId, Pageable pageable) {

		// SimpleQuery query = new SimpleQuery();

		keyword = ClientUtils.escapeQueryChars(keyword);

		HighlightQuery query = new SimpleHighlightQuery();

		query.addFilterQuery(new SimpleFilterQuery(new Criteria("classType")
				.is("SolrSrcServiceDTO")));

		String queryString = "";

		queryString += "projectId:" + projectId + " AND ( name:" + keyword
				+ " OR contents:" + keyword + " OR description:" + keyword
				+ " OR keyword:" + keyword + " OR deliverables:" + keyword
				+ " )";

		SimpleStringCriteria c = new SimpleStringCriteria(queryString);

		//
		// c.or(new SimpleStringCriteria("contents:" + keyword));
		// c.or(new SimpleStringCriteria("description:" + keyword));
		// c.or(new SimpleStringCriteria("keyword:" + keyword));
		// c.or(new SimpleStringCriteria("deliverables:" + keyword));
		//
		query.addCriteria(c);
		query.setPageRequest(pageable);

		return solrSrcServiceRepository.searchByKeyword(query);

	}

}
